home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / wrblocks / wrblocks.c < prev   
Encoding:
C/C++ Source or Header  |  1988-12-16  |  4.3 KB  |  190 lines

  1. /*
  2.  * wrblocks - create a file by writing a specified number of blocks to it.
  3.  */
  4. #include "sys/file.h"
  5. #include "sig.h"
  6. #include "stdio.h"
  7. #include "option.h"
  8. #include "errno.h"
  9. #include "spriteTime.h"
  10.  
  11. /*
  12.  * Options set by command line arguments
  13.  */
  14. int errorTest = 0;
  15. int numKbytes = 10;
  16. int blockSize = 16;
  17.  
  18. Option optionArray[] = {
  19.     OPT_INT,  "e", (Address)&errorTest, "Do write() error cases",
  20.     OPT_INT,  "k", (Address)&numKbytes, "Number of Kbytes for the file",
  21.     OPT_INT,  "b", (Address)&blockSize, "Blocksize of writes, in Kbytes",
  22. };
  23. int numOptions = sizeof(optionArray) / sizeof(Option);
  24.  
  25.  
  26. int Handler();
  27. int gotSig = FALSE;
  28.  
  29.  
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * main --
  35.  *
  36.  *    Open the specified file and writes numKbytes 1K blocks to it.
  37.  *
  38.  * Results:
  39.  *    None.
  40.  *
  41.  * Side effects:
  42.  *    Writes the file.
  43.  *
  44.  *----------------------------------------------------------------------
  45.  */
  46. main(argc, argv)
  47.     int argc;
  48.     char *argv[];
  49. {
  50.     int openFileID;
  51.     char *fileName;
  52.     char *buffer;
  53.     int total, bytesWritten;
  54.     Sig_Action        newAction, oldAction;
  55.     Time before, after;
  56.     double rate, tmp;
  57.     int k;
  58.  
  59.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  60.  
  61.     if (argc < 2) {
  62.     openFileID = 1;
  63.     } else {
  64.     fileName = argv[1];
  65.  
  66.     openFileID = open(fileName, O_CREAT|O_WRONLY|O_TRUNC, 0666);
  67.     if (openFileID < 0) {
  68.         fprintf(stderr, "Can't open \"%s\"\n", fileName);
  69.         perror("");
  70.         exit(1);
  71.     }
  72.     }
  73.     /*
  74.      * Set up signal handling, trap interrupts in order to test
  75.      * the GEN_INTERRUPTED_BY_SIGNAL return code.
  76.      */
  77.     newAction.action = SIG_HANDLE_ACTION;
  78.     newAction.handler = Handler;
  79.     newAction.sigHoldMask = 0;
  80.     Sig_SetAction(SIG_INTERRUPT, &newAction, &oldAction);
  81.  
  82.     buffer = (char *)malloc(1024 * blockSize);
  83.     bzero((Address)buffer, 1024 * blockSize);
  84.  
  85.     if (!errorTest) {
  86.     total = 0;
  87.     Sys_GetTimeOfDay(&before, NULL, NULL);
  88.     for (k=0 ; k<numKbytes ; k += blockSize) {
  89.         bytesWritten = write(openFileID, buffer, 1024 * blockSize);
  90.         if (bytesWritten < 0) {
  91.         perror("Write failed:");
  92.         break;
  93.         }
  94.         total += bytesWritten;
  95.         if (bytesWritten < 1024 * blockSize) {
  96.         fprintf(stderr, "Short write (%d not %d)\n",
  97.             bytesWritten, 1024 * blockSize);
  98.         break;
  99.         }
  100.     }
  101.     Sys_GetTimeOfDay(&after, NULL, NULL);
  102.     rate = after.seconds - before.seconds;
  103.     rate += (after.microseconds - before.microseconds)*.000001;
  104.     rate = total/rate;
  105.     fprintf(stderr,"%d bytes written at %.0f bytes/sec.\n", total, rate);
  106.     exit(errno);
  107.     } else {
  108.     int numErrors = 0;
  109.     printf("Write Error Tests\n");
  110.  
  111.     bytesWritten = write(-2, 0, 0);
  112.     if (bytesWritten >= 0) {
  113.         printf("ERROR: write(-2) worked!\n");
  114.         numErrors++;
  115.     } else {
  116.         perror("write(-2)");
  117.     }
  118.  
  119.     bytesWritten = write(openFileID, -1, 10);
  120.     if (bytesWritten >= 0) {
  121.         printf("ERROR: write{buffer = -1} worked!\n");
  122.         numErrors++;
  123.     } else {
  124.         perror("write{buffer = -1}");
  125.     }
  126.  
  127.     bytesWritten = write(openFileID, buffer, -1);
  128.     if (bytesWritten >= 0) {
  129.         printf("ERROR: write{count < 0} worked!\n");
  130.         numErrors++;
  131.     } else {
  132.         perror("write{count < 0}");
  133.     }
  134.  
  135.     {
  136.         int readOnlyFD;
  137.         readOnlyFD = open("/dev/null", O_RDONLY, 0);
  138.         if (readOnlyFD < 0) {
  139.         perror("Can't open \"/dev/null\"\n");
  140.         } else {
  141.         bytesWritten = write(readOnlyFD, buffer, 10);
  142.         if (bytesWritten >= 0) {
  143.             printf("ERROR: write{readonly stream} worked!\n");
  144.             numErrors++;
  145.         } else {
  146.             perror("write{readonly stream}");
  147.         }
  148.         }
  149.     }
  150.  
  151.     {
  152.         char *newBuf = (char *)malloc(100 * 1024);
  153.         ReturnStatus status;
  154.  
  155.         printf("Starting 100K write... "); fflush(stdout);
  156.         status = Fs_RawWrite(openFileID, 100 * 1024, newBuf, &bytesWritten);
  157.         if (gotSig) {
  158.         printf("Got Signal, "); fflush(stdout);
  159.         }
  160.         if (bytesWritten >= 0) {
  161.         printf("Wrote %d bytes\n", bytesWritten);
  162.         } else {
  163.         printf("write status 0x%x\n", status);
  164.         }
  165.     }
  166.  
  167.     close(openFileID);
  168.     bytesWritten = write(openFileID, sizeof("oops"), "oops");
  169.     if (bytesWritten >= 0) {
  170.         printf("ERROR: write{closed stream} worked!\n");
  171.         numErrors++;
  172.     } else {
  173.         perror("write{closed stream}");
  174.     }
  175.     if (numErrors) {
  176.         printf("Write Test had %d errors\n", numErrors);
  177.     } else {
  178.         printf("No errors\n");
  179.     }
  180.     exit(numErrors);
  181.     }
  182. }
  183.  
  184. int
  185. Handler()
  186. {
  187.     gotSig = TRUE;
  188. }
  189.  
  190.